package com.letoken.platform.repo.ecommerce.dao;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.letoken.platform.repo.app.req.mall.EvaluateListReq;
import com.letoken.platform.repo.app.req.mall.MyEvaluateReq;
import com.letoken.platform.repo.app.req.mall.UserQueryEvaluateReq;
import com.letoken.platform.repo.app.res.mall.DetailEvaluateRes;
import com.letoken.platform.repo.app.res.mall.EvaluateListRes;
import com.letoken.platform.repo.app.res.mall.MyEvaluateRes;
import com.letoken.platform.repo.app.res.mall.TaskScoreVo;
import com.letoken.platform.repo.dao.SpiceBaseMapper;
import com.letoken.platform.repo.ecommerce.dto.ShopEvaluateReq;
import com.letoken.platform.repo.ecommerce.dto.UserEvaluateReq;
import com.letoken.platform.repo.ecommerce.po.EcommerceUserEvaluate;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.letoken.platform.repo.ecommerce.po.EcommerceUserOrder;
import com.letoken.platform.repo.ecommerce.vo.*;
import com.letoken.platform.repo.mall.po.AppMallEvaluate;
import com.letoken.platform.repo.mall.po.AppMallOrderInfo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 订单评价表 Mapper 接口
 * </p>
 *
 * @author author
 * @since 2023-11-21
 */
public interface EcommerceUserEvaluateMapper extends SpiceBaseMapper<EcommerceUserEvaluate>  {

    @Select("select id,mall_id,user_id,order_id,score,anonymous from ecommerce_user_evaluate where id = #{id} and  user_id = #{userId} and state = 0 ")
    EcommerceUserEvaluate evaluateIdIsExits(@Param("userId") Integer userId, @Param("id") Integer id);


    @Select("select id,mall_id,order_id from ecommerce_user_evaluate where id = #{id} and  mall_id = #{mallId} and state = 0  and reply_state = 0 ")
    EcommerceUserEvaluate evaluateMallIdIsExits(@Param("mallId") Integer mallId, @Param("id") Integer id);



    @Select("select count(*) from ecommerce_user_evaluate where order_id = #{orderId} and  user_id = #{userId} limit 1")
    int evaluateCount(@Param("userId") Integer userId, @Param("orderId") Integer orderId);

    @Select("select father_no, id,mall_id,user_id,product_id from ecommerce_user_order where id = #{id} and  user_id = #{userId} ")
    EcommerceUserOrder evaluateOrder(@Param("userId") Integer userId, @Param("id") Integer id);


    @Select("select 1 from ecommerce_user_evaluate where order_id = #{orderId} and  state = 0 limit 1 ")
    Integer evaluateIsExits(@Param("orderId") Integer orderId);



    Page<ShopEvaluateRes> shopEvaluate(@Param("page") Page<ShopEvaluateRes> page , @Param("req") ShopEvaluateReq req);

    Page<UserEvaluateRes> userEvaluate(@Param("page") Page<UserEvaluateRes> page , @Param("req") UserEvaluateReq req);


    @Select("select p.score,p.id as productId, " +
            "(select count(1) from ecommerce_user_evaluate e where e.product_id = #{productId} ) as num ," +
            "(select count(1) from ecommerce_user_evaluate e where e.product_id = #{productId} and e.score < 3 ) as badNum, " +
            "(select sum(e.media_num) from ecommerce_user_evaluate e where e.product_id = #{productId} and e.media_num >0  ) as mediaNum " +
            "from ecommerce_product p where p.id = #{productId} ")
    UserEvaluateProductOverviewRes overview(@Param("productId") Integer productId);

    Page<UserEvaluateProductRes> productEvaluate(@Param("page") Page<UserEvaluateProductRes> page , @Param("req") UserEvaluateReq req);


    @Select(" select e.video_url,e.cover_url,e.id,u.head_portrait,u.user_name,e.score,e.content, e.create_time ,e.pics from ecommerce_user_evaluate e  inner join app_user u on e.user_id = u.id  where  e.product_id  = #{productId} and  e.state = 0  and e.anonymous = 0 order by e.create_time desc limit 2")
    List<UserProductEvaluateRes.EvaluateSimpleVo> productEvaluateTow(@Param("productId") Integer productId);

    @Select("select sum(score)/count(1) from ecommerce_user_evaluate where mall_id = #{mallId} and state in (0,2)")
    Float averageScore(@Param("mallId") Integer mallId);

    @Select("select count(1) from ecommerce_user_evaluate where mall_id = #{mallId} and state = 0")
    Integer evaluateNum(@Param("mallId") Integer mallId);


    @Select("select count(1) from ecommerce_user_evaluate where mall_id = #{mallId} and score >= #{score} and state = 0")
    Integer evaluateNumByScore(@Param("mallId") Integer mallId,@Param("score") Float score);

    @Select("select count(1) from ecommerce_user_evaluate where mall_id = #{mallId} and create_time > #{date} and state = 0")
    Integer dayEvaluateNum(@Param("mallId") Integer mallId, @Param("date") LocalDateTime date);



    Integer conditionEvaluateNum( @Param("req") EvaluateListReq req);

    Page<EvaluateListRes> evaluateList(@Param("page") Page<EvaluateListRes> page , @Param("req") EvaluateListReq req);


    @Select("select e.mall_id as mallId ,sum(e.score) as score ,m.is_business as isBusiness,m.level as level,m.level_score as levelScore from ecommerce_user_evaluate e inner join app_mall_info m on e.mall_id = m.id where e.state in (0,2) group by e.mall_id ")
    List<TaskScoreVo> allMallEvaluateScore();



    Page<DetailEvaluateRes> userQueryEvaluateList(@Param("page") Page<DetailEvaluateRes> page , @Param("req") UserQueryEvaluateReq req);


    @Select("select  (select count(*) from ecommerce_user_evaluate where mall_id = #{mallId} and score >= 3.0 and state in (0,2) ) / (select count(*) from app_mall_evaluate where mall_id = #{mallId} and state in (0,2) )")
    Float satisfaction(@Param("mallId") Integer mallId);



    @Select("select o.* from  ecommerce_user_order o inner join  ecommerce_order_detail  d on o.id = d.id   where d.receive_time <= #{endTime} and o.status = 3 and  o.reviewed = 0  ")
    List<EcommerceUserOrder> waitEvaluate(@Param("endTime") LocalDateTime endTime);


}



